package com.sks.web.action.house;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.stereotype.Controller;

import com.sks.bean.pojo.SksInfo;
import com.sks.core.SystemConstants;
import com.sks.service.base.SksInfoService;
import com.sks.web.action.BasicAction;
import com.sks.web.action.privilege.Permission;
import com.sks.web.formbean.house.MessageForm;

@Controller("/cms/info")
public class SksInfoAction extends BasicAction {
	@Resource(name = "sksInfoServiceBean") private SksInfoService service;
	
	@Permission(module = "info", privilege = "view")
	public ActionForward editUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MessageForm formbean = (MessageForm) form;
		String sksKey = request.getParameter("sksKey");
		SksInfo info = null;
		if (SystemConstants.SKS_ABOUT_US.equals(sksKey)) {
			info = service.getAboutUs();
		} else if (SystemConstants.SKS_INTRODUCE.equals(sksKey)) {
			info = service.getSksIntroduce();
		} else {
			request.setAttribute(MESSAGE, "页面出错");
			return mapping.findForward(MESSAGE);
		}
		
		request.setAttribute("sksKey", sksKey);
		formbean.setInfo(null != info ? info.getInfo() : "");
		this.setReadOnly(request, "editUI");
		return mapping.findForward(EDIT);
	}
	
	@Permission(module = "info", privilege = "update")
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MessageForm formbean = (MessageForm) form;
		String sksKey = request.getParameter("sksKey");
		SksInfo info = null;
		if (SystemConstants.SKS_ABOUT_US.equals(sksKey)) {
			info = service.getAboutUs();
			if (null == info) {
				info = new SksInfo(SystemConstants.SKS_ABOUT_US, formbean.getInfo());
				service.save(info);
			} else {
				info.setInfo(formbean.getInfo());
				service.update(info);
			}
		} else if (SystemConstants.SKS_INTRODUCE.equals(sksKey)) {
			info = service.getSksIntroduce();
			if (null == info) {
				info = new SksInfo(SystemConstants.SKS_INTRODUCE, formbean.getInfo());
				service.save(info);
			} else {
				info.setInfo(formbean.getInfo());
				service.update(info);
			}
		} else {
			request.setAttribute(MESSAGE, "页面出错");
			return mapping.findForward(MESSAGE);
		}
		
		return editUI(mapping, formbean, request, response);
	}
}
